<template>
    <div>
      <el-button type="primary" style="margin-bottom: 10px;margin-top: 10px;margin-left: 5px" @click="dialogVisible = true">添加日报</el-button>
      <el-table
        :data="dailys"
        border
        style="width: 100%">
        <el-table-column
          fixed
          prop="daliyData"
          label="日期"
          width="150">
        </el-table-column>
        <el-table-column
          prop="daliyDetail"
          label="内容"
          width="1070px">
        </el-table-column>
        <el-table-column
          label="操作"
          width="100">
          <template slot-scope="scope">
            <el-button @click="handleDel(scope.row)" type="text" size="small">删除</el-button>
            <el-button type="text" size="small" @click="showDaily(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="currentChange"
        :current-page="currentPage"
        background
        :page-size="10"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
      <el-dialog
        title="编辑日报"
        @close="dialogVisibleClose"
        :visible.sync="dialogVisible"
        width="30%">
        <el-form :model="daily" label-width="80px">
          <el-form-item label="时间">
            <el-date-picker
              v-model="daily.daliyData"
              type="date"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="日报内容">
            <el-input
              type="textarea"
              :rows="3"
              placeholder="请输入内容"
              v-model="daily.daliyDetail">
            </el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" style="margin-right: 5px">取 消</el-button>
          <el-button type="primary" @click="addOrUpdateDaily">确定</el-button>
        </span>
      </el-dialog>
    </div>
</template>

<script>
import { initDaily, add, update, del } from '../../../api/stu/daily'

export default {
  data() {
    return {
      dialogVisible: false,
      currentPage: 1,
      dailys: [{
        dailyId: 0,
        daliyData: '',
        daliyDetail: ''
      }],
      total: 100,
      daily: {
        dailyId: 0,
        daliyData: '',
        daliyDetail: ''
      }
    }
  },
  methods: {
    // 初始化页面数据
    init() {
      initDaily(this.currentPage).then(response =>{
        console.log(response)
        this.dailys = response.data.records
        this.total = response.data.total
      }).catch(error=>{
        this.$message.error(error.msg)
      })
    },
    // 获取当前页信息
    currentChange(page) {
      this.currentPage = page
      this.init(page)
    },
    // 删除日报
    handleDel(row) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.dailyId).then(response=>{
          this.$message({
            message:response.msg,
            type: 'success'
          })
          this.currentChange(this.currentPage)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 更新或添加日报
    addOrUpdateDaily() {
      if (this.daily.dailyId > 0){
        // 修改
        update(this.daily).then(response => {
          this.currentChange(this.currentPage);
          this.$message({
            message: response.msg,
            type: 'success'
          });
          console.log(response)
        })
      }else {
        // 添加
        add(this.daily).then(response => {
          this.currentChange(this.currentPage);
          this.$message({
            message: response.msg,
            type: 'success'
          });
          console.log(response)
        }).catch(error => {
          console.log(error)
        })
      }
      this.dialogVisible = false
    },
    // 编辑回显数据
    showDaily(row) {
      console.log(row)
      this.daily = row
      this.dialogVisible = true
    },
    // 对话框关闭回调
    dialogVisibleClose() {
      this.daily = {
        daliyData: '',
        daliyDetail: ''
      }
    }
  },
  created() {
    this.init()
  }
}
</script>

<style scoped>

</style>
